package com.sxkf.template.controller;


import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.User;
import com.sxkf.template.service.LoginService;
import com.sxkf.template.vo.request.user.UserLoginVo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @author yyl25
 */
@Api(tags = "登录模块")
@RestController
@RequestMapping("/api")
public class UserLoginController {
    @Resource
    private LoginService loginService;

    @ApiOperation(value = "用户登录",notes = "微信登录")
    @PostMapping("/wxLogin")
    public R<UserLoginVo> wxLogin(@RequestParam @ApiParam("openid授权码") String openCode,
                                  @RequestParam @ApiParam("手机号授权码") String phoneCode) {
        return R.success(loginService.wxLogin(openCode,phoneCode));
    }

    @ApiOperation(value = "用户登录",notes = "手机号验证码登录")
    @PostMapping("/telLogin")
    public R<UserLoginVo> telLogin(@RequestParam @ApiParam("手机号") String phoneNumber,
                                   @RequestParam @ApiParam("验证码") String captcha) {
        return R.success(loginService.telLogin(phoneNumber,captcha));
    }

    @ApiOperation(value = "用户登录",notes = "手机号密码登录")
    @PostMapping("/pwdLogin")
    public R<UserLoginVo> pwdLogin(@RequestParam String phoneNumber,
                                   @RequestParam String password){
        return R.success(loginService.pwdLogin(phoneNumber,password));
    }

    @ApiOperation(value = "获取验证码")
    @GetMapping(value = "/sendCaptcha")
    public R<String> sendCaptcha(@RequestParam String phoneNumber){
        loginService.sendCaptcha(phoneNumber);
        return R.success();
    }

    @ApiOperation(value = "忘记密码")
    @PostMapping(value = "/forgetPass")
    public R<String> forgetPass(@RequestParam String phoneNumber,
                                 @RequestParam String captcha,
                                 @RequestParam String newPass){
        Boolean res = loginService.forgetPass(phoneNumber,captcha,newPass);
        if (res){
            return R.success("密码重置成功");
        }
        return R.error("密码重置失败");
    }

    @ApiOperation(value = "获取忘记密码验证码")
    @GetMapping(value = "/sendForgetCaptcha")
    public R<String> sendForgetCaptcha(@RequestParam String phoneNumber){
        loginService.sendForgetCaptcha(phoneNumber);
        return R.success();
    }

    @ApiOperation("用户注册")
    @PostMapping("/userRegister")
    public R<UserLoginVo> userRegister(@RequestBody User user){
        return R.success(loginService.register(user));
    }

    @ApiOperation("登出(注销)")
    @GetMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        return R.success(loginService.logout(request));
    }

}
